package com.svms.save.utils;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSONObject;
import com.svms.save.vo.ResultVo;

public class WebUtils {
	public static final String CONTENTTYPE_TEXTHTML = "text/html";
	public static final String CONTENTTYPE_TEXTJSON = "text/json";
	public static final String CONTENTTYPE_TEXTXML = "text/xml";
	public static final String CONTENT_CHARSET_GBK = "GBK";
	public static final String CONTENT_CHARSET_GB2312 = "GB2312";
	public static final String CONTENT_CHARSET_UTF8 = "UTF-8";
	
	private WebUtils() {
	}

	
	/**
	 * 直接通过<b>{@link HttpServletResponse}写数据到客户端</b>,可以指定编码,文件类型
	 * @param response {@link HttpServletResponse}
	 * @param contentType 文件类型
	 * @param charset 编码集
	 * @param s 数据
	 */
	public static void sendDirectToClient(HttpServletResponse response, String contentType, String charset, String s) {
		String charsetPrefix = org.springframework.web.util.WebUtils.CONTENT_TYPE_CHARSET_PREFIX;
		String contentHead = contentType + charsetPrefix + charset;
		response.setContentType(contentHead);
		try {
			PrintWriter writer = response.getWriter();
			writer.write(s);
			writer.flush();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	public static void sendDirectToClient(HttpServletResponse response,ResultVo result) {
		String charsetPrefix = org.springframework.web.util.WebUtils.CONTENT_TYPE_CHARSET_PREFIX;
		String contentHead = CONTENTTYPE_TEXTJSON + charsetPrefix + CONTENT_CHARSET_UTF8;
		response.setContentType(contentHead);
		try {
			String json = JSONObject.toJSONString(result);
			PrintWriter writer = response.getWriter();
			writer.write(json);
			writer.flush();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}
